package com.skyline.wo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.skyline.base.exception.NotLoginException;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.User;
import com.skyline.wo.service.InviteService;

@Controller
@RequestMapping("/invite")
public class InviteController {

	@Autowired
	private InviteService inviteService;

//	@Value("${view.invite.request}")
//	private String inviteRequestView;
//
////	@Value("${view.invite.success}")
//	private String inviteSuccessView;

	@RequestMapping(value = "/joinUs", method = RequestMethod.GET)
	public ModelAndView inviteRequest() {
		ModelAndView mav = new ModelAndView();
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user == null || user.getId() == 0) {
			throw new NotLoginException();
		} else {
			mav.setViewName(ViewPaths.INVITE_REQUEST);
		}
		return mav;
	}

	@RequestMapping(value = "/joinUs", method = RequestMethod.POST)
	public ModelAndView inviteExecute(String[] emails,int valideEmailCount) {
		ModelAndView mav = new ModelAndView();
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user == null || user.getId() == 0) {
			throw new NotLoginException();
		} else {
			//System.out.println(valideEmailCount);
			inviteService.sendInvitation(emails, user,valideEmailCount);
			mav.setViewName(ViewPaths.INVITE_SUCCESS);
		}
		return mav;
	}
}
